<template>
  <div class="enterprise-container">
    <div class="search">
      <el-form :inline="true">
        <el-form-item label="楼宇名称">
          <el-input v-model="query.name" placeholder="请输入楼宇名称" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="medium" @click="search">查询</el-button>
        </el-form-item>
      </el-form>
      <el-button type="primary" size="medium" @click="dialogFormVisible = true">添加楼宇</el-button>
    </div>
    <el-table :data="buildingList">
      <el-table-column type="index" label="序号" />
      <el-table-column prop="name" label="楼宇名称" />
      <el-table-column prop="floors" label="层数" />
      <el-table-column prop="area" label="在管面积" />
      <el-table-column prop="propertyFeePrice" label="物业费" />
      <el-table-column prop="status" label="状态" :formatter="formatStatus" />
      <el-table-column label="操作">
        <template v-slot="scope">
          <el-button type="text" size="mini" @click="editBuilding(scope.row)">编辑</el-button>
          <el-button type="text" size="mini" @click="delBuilding(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="text-align: right;"
      :current-page="query.page"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="query.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <el-dialog :title="isEdit ? '编辑楼宇' : '添加楼宇'" :visible.sync="dialogFormVisible" width="30%">
      <el-form ref="buildingForm" class="buildingForm" :model="buildingForm" :rules="buildingRules">
        <el-form-item prop="name" label="楼宇名称">
          <el-input v-model="buildingForm.name" placeholder="请输入楼宇名称" />
        </el-form-item>
        <el-form-item prop="floors" label="楼宇层数">
          <el-input v-model="buildingForm.floors" placeholder="请输入楼宇层数" />
        </el-form-item>
        <el-form-item prop="area" label="在管面积">
          <el-input v-model="buildingForm.area" placeholder="请输入在管面积">
            <template slot="append">m²</template>
          </el-input>
        </el-form-item>
        <el-form-item prop="propertyFeePrice" label="物业费">
          <el-input v-model="buildingForm.propertyFeePrice" placeholder="请输入物业费">
            <template slot="append">元/m²</template>
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getBuildingListAPI, addBuildingAPI, updateBuildingAPI, delBuildingAPI } from '@/apis/building'

export default {
  name: 'Building',
  data() {
    return {
      buildingList: [],
      query: {
        name: '',
        page: 1,
        pageSize: 10
      },
      total: 0,
      dialogFormVisible: false,
      buildingForm: {
        name: '办公室X',
        floors: '20',
        area: '500',
        propertyFeePrice: '2.5'
      },
      buildingRules: {
        name: [{ required: true, message: '请输入楼宇名称', trigger: 'blur' }],
        floors: [{ required: true, message: '请输入楼宇层数', trigger: 'blur' }],
        area: [{ required: true, message: '请输入在管面积', trigger: 'blur' }],
        propertyFeePrice: [{ required: true, message: '请输入物业费', trigger: 'blur' }]
      },
      isEdit: false
    }
  },
  created() {
    this.getBuildingList()
  },
  methods: {
    // 查询楼宇
    search() {
      this.query.page = 1
      this.getBuildingList()
    },
    // 确认添加/修改楼宇
    addSubmit() {
      this.$refs.buildingForm.validate(async(valid) => {
        if (valid) {
          if (this.isEdit) {
            await updateBuildingAPI(this.buildingForm)
            this.getBuildingList()
            this.$message.success('修改成功')
          } else {
            await addBuildingAPI(this.buildingForm)
            this.getBuildingList()
            this.$message.success('添加成功')
          }
          this.buildingForm = {
            name: '办公室X',
            floors: '20',
            area: '500',
            propertyFeePrice: '2.5'
          }
        }
        this.dialogFormVisible = false
        this.isEdit = false
      })
    },

    // 删除楼宇
    delBuilding(id) {
      this.$confirm('是否确认永久删除楼宇信息', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消'
      }).then(async() => {
        await delBuildingAPI(id)
        this.$message.success('成功删除')
        this.getBuildingList()
      }).catch()
    },
    // 编辑楼宇
    editBuilding(row) {
      this.dialogFormVisible = true
      this.isEdit = 1
      const { name, floors, area, propertyFeePrice, id } = row
      this.buildingForm = { id, name, floors, area, propertyFeePrice }
    },

    // 楼宇状态格式转换
    formatStatus(row) {
      const MAP = {
        '0': '空置中',
        '1': '租赁中'
      }
      return MAP[row.status]
    },
    // 获取楼宇列表
    async getBuildingList() {
      const res = await getBuildingListAPI(this.query)
      this.buildingList = res.data.rows
      this.total = res.data.total
    },
    // 分页方法
    handleSizeChange(pageSize) {
      this.query.pageSize = pageSize
      this.getBuildingList()
    },
    handleCurrentChange(page) {
      // 把点击的页数赋值给请求参数页数
      this.query.page = page
      console.log(this.query.page)
      // 使用最新的请求参数获取列表数据
      this.getBuildingList()
    }
  }
}
</script>

<style lang="scss" scoped>
.buildingForm {
  display: flex;
  flex-direction: column;
  align-items: center;

  .el-form-item {
    width: 400px;
  }
}
</style>
